﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Web.Security;
using BusinessLayer;
using System.IO;

namespace Design
{
    public partial class WebForm14 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.IsInRole("administrator") && !User.IsInRole("manager") && !User.IsInRole("employee"))
            {
                Response.Redirect("/Login.aspx");
            }
        }

        private void AssignRoles()
        {
            try
            {
                if (radgeneralemp.Checked == false && radmanager.Checked == false)
                {
                    MessageBox.Show(null, "Please Select User Role", "User Role", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                    if (radmanager.Checked == true)
                    {
                        Roles.AddUserToRole(CreateUserWizard1.UserName, "manager");
                    }
                    else
                        if (radgeneralemp.Checked == true)
                        {
                            Roles.AddUserToRole(CreateUserWizard1.UserName, "employee");
                        }

            }
            catch (Exception ex)
            {

            }
        }

        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            AssignRoles();

            SignUp objadduser = new SignUp();

            objadduser.username = CreateUserWizard1.UserName.ToString();
            objadduser.password = CreateUserWizard1.Password.ToString();
            objadduser.email = CreateUserWizard1.Email.ToString();
            objadduser.type = "administrator";
            objadduser.signup_date = DateTime.Now;

            objadduser.add_user();
        }

        protected void btnUpdateEquip_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Server.MapPath("EquipmentImages/");
                if (fluUploadImage.HasFile)
                {
                    string ext = Path.GetExtension(fluUploadImage.FileName);
                    if (ext == ".jpg" || ext == ".png" || ext == ".jpeg")
                    {
                        fluUploadImage.SaveAs(path + fluUploadImage.FileName);
                        string imagename = "~/EquipmentImages/" + fluUploadImage.FileName;

                        UpdateEquip objUpdateEquip = new UpdateEquip();

                        objUpdateEquip.Equip_ID = txtEquipID.Text;
                        objUpdateEquip.Name = txtEquipName.Text;
                        objUpdateEquip.Price = Convert.ToDecimal(txtEquipPrice.Text);
                        objUpdateEquip.Description = txtEquipDesc.Text;
                        objUpdateEquip.image = imagename;
                        objUpdateEquip.Category = drpCategory.SelectedValue;

                        objUpdateEquip.update_equipment();

                        MessageBox.Show("Equipment Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Only .JPG, .JPEG and .YPNG Files Allowed!", "Extension Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("Please Select A Picture!", "Image Selection", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch
            {
                MessageBox.Show("Enter Infomration To Update", "Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        protected void btnAddEquip_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Server.MapPath("EquipmentImages/");
                if (fluUploadImage.HasFile)
                {
                    string ext = Path.GetExtension(fluUploadImage.FileName);
                    if (ext == ".jpg" || ext == ".png")
                    {
                        fluUploadImage.SaveAs(path + fluUploadImage.FileName);
                        string imagename = "~/EquipmentImages/" + fluUploadImage.FileName;

                        AddEquip Equip = new AddEquip();

                        Equip.Equip_ID = txtEquipID.Text;
                        Equip.Name = txtEquipName.Text;
                        Equip.Price = Convert.ToDecimal(txtEquipPrice.Text);
                        Equip.Description = txtEquipDesc.Text;
                        Equip.Category = drpCategory.Text;
                        Equip.image = imagename;

                        Equip.save_equipment();

                        MessageBox.Show("Equipment Added", "Add Equipment", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                    else
                    {

                    }
                }
                else
                {

                }
            }
            catch
            {

            }
        }

        protected void btnDeleteEquip_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateEquip equip_del = new UpdateEquip();
                equip_del.Equip_ID = txtEquipID.Text;
                equip_del.delete_equipment();

                txtEquipID.Text = "";
                txtEquipName.Text = "";
                txtEquipDesc.Text = "";
                txtEquipPrice.Text = "";
                imgDisplay.ImageUrl = "";
            }
            catch
            {
                MessageBox.Show("Enter the Equipment ID to delete", "Delete Equipment", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        protected void btnSearchEquip_Click(object sender, EventArgs e)
        {
            UpdateEquip equip = new UpdateEquip();
            List<UpdateEquip> objEquip = new List<UpdateEquip>();
            objEquip = equip.get_equipment_even_by_id();

            int count = objEquip.Count();
            for (int i = 0; i < count; i++)
            {
                if (txtEquipID.Text == objEquip[i].Equip_ID)
                {
                    try
                    {
                        txtEquipID.Text = objEquip[i].Equip_ID;
                        txtEquipName.Text = objEquip[i].Name;
                        txtEquipPrice.Text = Convert.ToString(objEquip[i].Price);
                        txtEquipDesc.Text = objEquip[i].Description;
                        imgDisplay.ImageUrl = objEquip[i].image;
                        drpCategory.SelectedValue = objEquip[i].Category;
                    }
                    catch
                    {
                        MessageBox.Show("Equipment does not exist!", "Search Equipment", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        protected void btnAddNews_Click(object sender, EventArgs e)
        {
            try
            {
                News Add_News = new News();
                Add_News.News_id = txtNews.Text;
                Add_News.add_News();

                txtNews.Text = "";
                txtNewsDescript.Text = "";
            }
            catch
            {
                MessageBox.Show("News Added", "Add News", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }

        protected void btnDeleteNews_Click(object sender, EventArgs e)
        {
            try
            {
                News Delete_news = new News();
                Delete_news.News_id = txtNews.Text;
                Delete_news.delete_News();

                txtNews.Text = "";
                txtNewsDescript.Text = "";


            }
            catch
            {
                MessageBox.Show("News Deleted", "Delete News", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        protected void btnSaveNews_Click(object sender, EventArgs e)
        {
            try
            {
                News Add_News = new News();
                Add_News.News_id = txtNews.Text;
                Add_News.add_News();

                txtNews.Text = "";
                txtNewsDescript.Text = "";
            }
            catch
            {
                MessageBox.Show("News Saved", "Save News", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        protected void btnSearchCatal_Click(object sender, EventArgs e)
        {
        
                Catalogue Sercat = new Catalogue();
                List<Catalogue> obj_Sercat = new List<Catalogue>();
                obj_Sercat = Sercat.get_Catal(); 
                
                int count = obj_Sercat.Count();
                for (int i = 0; i < count; i++)
                {
                    if (txtCataID.Text == obj_Sercat[i].cat)
                    {
                        try
                        {
                            txtCataID.Text = obj_Sercat[i].cat;
                            txtEqID.Text = obj_Sercat[i].equip;
                            txtSeasonalRate.Text = obj_Sercat[i].season_r;
                            lblstatD.Text = Convert.ToString(obj_Sercat[i].startD);
                            lblendD.Text = Convert.ToString(obj_Sercat[i].endD);
                            txtSeason.Text = Convert.ToString(obj_Sercat[i].season);

                            
                        }
                        catch
                        {
                            MessageBox.Show("Catalogue does not exist!", "Search Catalogue", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }

        }
       
        protected void btnAddCatal_Click(object sender, EventArgs e)
        {
            try
            {

                Catalogue obj_Addcat = new Catalogue();

                obj_Addcat.equip = txtEqID.Text;
                obj_Addcat.season_r = txtSeasonalRate.Text;
                obj_Addcat.startD = Convert.ToDateTime(calStartD.SelectedDate);
                obj_Addcat.endD = Convert.ToDateTime(calEndD.SelectedDate);
                obj_Addcat.season = Convert.ToUInt16(txtSeason.Text);
                obj_Addcat.sav_catalogue();

                
                MessageBox.Show(" Save Catalogue", "Save Catalogue", MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("Catalogue", "Catalogue not Saved", MessageBoxButtons.OK,MessageBoxIcon.Error );
            }
        }

        protected void btnUpdateCatal_Click(object sender, EventArgs e)
        {

            try
            {
                        Catalogue  obj_upcata = new Catalogue ();
                        
                        obj_upcata.season_r = txtSeasonalRate.Text;
                        obj_upcata.startD = Convert.ToDateTime (calStartD.SelectedDate );
                        obj_upcata.endD = Convert.ToDateTime (calEndD.SelectedDate );                        
                        obj_upcata.season = Convert.ToInt16 (txtSeason.Text );

                        obj_upcata.update_catal();

                        MessageBox.Show("Catalogue Updated", "Update Catalogue", MessageBoxButtons.OK, MessageBoxIcon.Information);
             }  
            catch
            {
                MessageBox.Show("Enter Infomration To Update catalogue", "Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


    }
}